package cn.bughub.util;

import cn.bughub.enums.StatusEnum;
import lombok.Getter;
import lombok.Setter;

import java.io.Serial;
import java.io.Serializable;

/**
 * 统一返回实体类
 *
 * @author zwj
 * @date 2024-09-23 17:12
 */
@Setter
@Getter
public class ResultResponse<T> {
    
    private Boolean success;
    
    private Integer code;
    
    private String message;
    
    private Long timestamp = System.currentTimeMillis();
    
    private T data;
    
    /**
     * 成功响应方法
     *
     * @param data 数据
     * @return {@link ResultResponse }<{@link T }>
     */
    public static <T> ResultResponse<T> success(T data) {
        
        ResultResponse<T> response = new ResultResponse<>();
        response.setData(data);
        response.setCode(StatusEnum.SUCCESS.code);
        response.setMessage(StatusEnum.SUCCESS.message);
        response.setSuccess(Boolean.TRUE);
        return response;
    }
    
    public static <T> ResultResponse<T> error(StatusEnum statusEnum) {
        
        return error(statusEnum, statusEnum.message);
    }
    
    /**
     * 自定义错误消息
     *
     * @param statusEnum   错误响应状态码
     * @param errorMessage 自定义错误信息
     * @return {@link ResultResponse }<{@link T }>
     */
    public static <T> ResultResponse<T> error(StatusEnum statusEnum, String errorMessage) {
        
        ResultResponse<T> response = new ResultResponse<>();
        response.setSuccess(Boolean.FALSE);
        response.setCode(statusEnum.code);
        response.setMessage(errorMessage);
        return response;
    }
}
